#include "BitmapFont.h"
#include "../SGD Wrappers/tinyxml.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include <string>
using namespace std;
CBitmapFont::CBitmapFont(void)
{
	m_nNumRows = 6;
	m_nNumCols.push_back(14);
	m_nNumCols.push_back(14);
	m_nNumCols.push_back(17);
	m_nNumCols.push_back(16);
	m_nNumCols.push_back(20);
	m_nNumCols.push_back(11);
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	m_nImageID = pTM->LoadTexture(L"resources/graphics/DeadlyShadowsFont_0.png");
	m_cFirstCharacter = '[';
}


CBitmapFont::~CBitmapFont(void)
{
}

bool CBitmapFont::Load(char const*const szFilename)
{
	TiXmlDocument doc;
	if(!doc.LoadFile(szFilename))
		return false;
	TiXmlElement* pRoot = doc.RootElement();
	if(!pRoot)
		return false;
	TiXmlElement* pChild = pRoot->FirstChildElement("info");
	while(pChild)
	{
		//info's data read in
		int bOutline;
		int spacingY, spacingX, paddingBottom, paddingRight, paddingTop, paddingLeft, aa, smooth, stretchH, unicode, italic, bold, size;
		string charset, face;
		//pChild->Attribute("outline", &This is where you put data member to be set);
		pChild->Attribute("outline", &bOutline);
		pChild->Attribute("spacingY", &spacingY);
		pChild->Attribute("spacingX", &spacingX);
		pChild->Attribute("paddingbottom", &paddingBottom);
		pChild->Attribute("paddingright", &paddingRight);
		pChild->Attribute("paddingtop", &paddingTop);
		pChild->Attribute("paddingleft", &paddingLeft);
		pChild->Attribute("aa", &aa);
		pChild->Attribute("smooth", &smooth);
		pChild->Attribute("stretchH", &stretchH);
		pChild->Attribute("unicode", &unicode);
		charset = pChild->Attribute("charset");
		pChild->Attribute("italic", &italic);
		pChild->Attribute("bold", &bold);
		pChild->Attribute("size", &size);
		face = pChild->Attribute("face");
		//This is second section
		pChild = pChild->NextSiblingElement("common");
		//Read in this one's data
		int blueChnl, greenChnl, redChnl, alphaChnl, packed, pages, scaleH, scaleW, base, lineHeight;
		pChild->Attribute("blueChnl", &blueChnl);
		pChild->Attribute("greenChnl", &greenChnl);
		pChild->Attribute("redChnl", &redChnl);
		pChild->Attribute("alphaChnl", &alphaChnl);
		pChild->Attribute("packed", &packed);
		pChild->Attribute("pages", &pages);
		pChild->Attribute("scaleH", &scaleH);
		pChild->Attribute("scaleW", &scaleW);
		pChild->Attribute("base", &base);
		pChild->Attribute("lineHeight", &lineHeight);
		pChild = pChild->NextSiblingElement("pages");
		TiXmlElement* pNode = pChild->FirstChildElement("page");
		//TODO Read in data
		string filename; int pageid;
		filename = pNode->Attribute("file");
		pNode->Attribute("id", &pageid);
		pChild = pChild->NextSiblingElement("chars");
		int count;
		pChild->Attribute("count" ,&count);
		/*TiXmlElement**/ pNode = pChild->FirstChildElement("char"); 
		int x, y, width, height, offsetX, offsetY, chnl, page, xAdvance, id;
		for(int i = 0; i < count; i++)
		{
			//Read in char data
			pNode->Attribute("id", &id);
			pNode->Attribute("chnl", &chnl);
			pNode->Attribute("page", &page);
			pNode->Attribute("xadvance", &xAdvance);
			pNode->Attribute("yoffset", &offsetY);
			pNode->Attribute("xoffset", &offsetX);
			pNode->Attribute("height", &height);
			pNode->Attribute("width", &width);
			pNode->Attribute("y", &y);
			pNode->Attribute("x", &x);
			CBitmapChar bmChar;
			bmChar.SetID(id);
			bmChar.SetChannel(chnl);
			bmChar.SetPage(page);
			bmChar.SetXAdvance(xAdvance);
			bmChar.SetYOffset(offsetY);
			bmChar.SetXOffset(offsetX);
			bmChar.SetHeight(height);
			bmChar.SetWidth(width);
			bmChar.SetY(y);
			bmChar.SetX(x);
			m_pBitmapChars.push_back(bmChar);
			pNode = pNode->NextSiblingElement("char");
		}
		break;
	}
	return true;
}

void CBitmapFont::Print(char const*const szText, int nX, int nY, float fScale, DWORD dwColor)
{
	int nColStart = nX;
	int nLength = strlen(szText);
	for(int i = 0; i < nLength; i++)
	{
		char ch = szText[i];
		if(ch == ' ')
		{
			nX += 8 * (int)fScale;
			continue;
		}
		else if(ch == '\n')
		{
			nX = nColStart;
			nY += 32 * (int)fScale;
			continue;
		}
		RECT rTile = {this->m_pBitmapChars[ch-32].GetX(), this->m_pBitmapChars[ch-32].GetY(), 
			this->m_pBitmapChars[ch-32].GetX() + this->m_pBitmapChars[ch-32].GetWidth(), 
			this->m_pBitmapChars[ch-32].GetY() + this->m_pBitmapChars[ch-32].GetHeight()};
		CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
		pTM->Draw(m_nImageID, nX + this->m_pBitmapChars[ch-32].GetXOffset()*fScale, nY + this->m_pBitmapChars[ch-32].getYOffset()*fScale, fScale, fScale, &rTile, 0.0f, 0.0f, 0.0f, dwColor); 
		nX += this->m_pBitmapChars[ch-32].GetWidth()*fScale;
	}
}